package br.com.upis.sispat.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import br.com.upis.sispat.dao.Dao;
import br.com.upis.sispat.entity.Patrimonio;
import br.com.upis.sispat.entity.Requisicao;
import br.com.upis.sispat.enums.StatusRequisicao;
import br.com.upis.sispat.exception.SeamCoreException;
import br.com.upis.sispat.service.PatrimonioService;
import br.com.upis.sispat.service.RequisicaoService;

@Service("patrimonioService")
public class PatrimonioServiceImpl implements PatrimonioService {

	private Dao<Patrimonio> patrimonioDao;
	private RequisicaoService requisicaoService;

	private static final long serialVersionUID = -3306452448892268624L;

	@SuppressWarnings("unchecked")
	public List<Patrimonio> search(Patrimonio patrimonio) throws SeamCoreException {

		StringBuilder hql = new StringBuilder();
		hql.append(" select u from Patrimonio u ");
		hql.append(" where 1=1 ");

		List<String> listaParam = new ArrayList<String>();

		if (patrimonio.getNomePatrimonio() != null
				&& patrimonio.getNomePatrimonio().length() > 0) {
			hql.append(" and u.nomePatrimonio like ? ");
			listaParam.add(patrimonio.getNomePatrimonio());
		}

		if (patrimonio.getTipoPatrimonio() != null && patrimonio.getTipoPatrimonio().length() > 0) {
			hql.append(" and u.tipoPatrimonio = ? ");
			listaParam.add(patrimonio.getTipoPatrimonio());
		}
		
		if (patrimonio.getCodigoPatrimonio() != null && patrimonio.getCodigoPatrimonio().length() > 0) {
			hql.append(" and u.codigoPatrimonio = ? ");
			listaParam.add(patrimonio.getCodigoPatrimonio());
		}
		
		if (patrimonio.getSetor().getCodigo() != null && patrimonio.getSetor().getCodigo() > 0) {
			hql.append(" and u.setor.codigo = ? ");
			listaParam.add(patrimonio.getCodigoPatrimonio());
		}

		int i = 0;

		Session session = patrimonioDao.getSession();

		Transaction tx = session.getTransaction();

		tx.begin();

		Query query = session.createQuery(hql.toString());

		if (patrimonio.getNomePatrimonio() != null
				&& patrimonio.getNomePatrimonio().length() > 0) {

			query.setString(i++, "%" + patrimonio.getNomePatrimonio() + "%");
		}

		if (patrimonio.getTipoPatrimonio() != null && patrimonio.getTipoPatrimonio().length() > 0) {
			query.setString(i++, patrimonio.getTipoPatrimonio());
		}
		
		if (patrimonio.getCodigoPatrimonio() != null && patrimonio.getCodigoPatrimonio().length() > 0) {
			query.setString(i++, patrimonio.getCodigoPatrimonio());
		}
		
		if (patrimonio.getSetor().getCodigo() != null && patrimonio.getSetor().getCodigo() > 0) {
			query.setString(i++, patrimonio.getSetor().getCodigo().toString());
		}

		List<Patrimonio> lista = query.list();

		tx.commit();

		return lista;
	}

	@Override
	@Transactional
	public void save(Patrimonio patrimonio) throws SeamCoreException {

		try {
			patrimonioDao.create(patrimonio);
		} catch (SQLException e) {
			throw new SeamCoreException(e);
		}

	}

	@Override
	@Transactional
	public void delete(Patrimonio patrimonio) throws SeamCoreException {
		try {
			patrimonioDao.delete(patrimonio);
		} catch (SQLException e) {
			throw new SeamCoreException(e);
		}

	}

	@Override
	@Transactional
	public void update(Patrimonio patrimonio) throws SeamCoreException {
		if(patrimonio.getSetor().getCodigo().intValue()==1){
			Requisicao requisicao = new Requisicao();
			requisicao.setPatrimonio(patrimonio);
			
			Requisicao requisicao2 = requisicaoService.search(requisicao).get(0);
			
			if(requisicao2!=null){
				requisicao2.setPatrimonio(null);
				requisicao2.setStatusRequisicao(StatusRequisicao.DEVOLVIDO.getCodigoRequisicao());
				requisicaoService.update(requisicao2);
			}
		}
		try {
			patrimonioDao.update(patrimonio);
		} catch (SQLException e) {
			throw new SeamCoreException(e);
		}

	}

	public Patrimonio getById(Long id) {

		return patrimonioDao.getObjectFromId(Patrimonio.class, id);
	}

	@Autowired
	public void setPatrimonioDao(Dao<Patrimonio> patrimonioDao) {
		this.patrimonioDao = patrimonioDao;
	}
	@Autowired
	public void setRequisicaoService(RequisicaoService requisicaoService) {
		this.requisicaoService = requisicaoService;
	}

}
